#ifndef __FILEPROCESSOR_H__
#define __FILEPROCESSOR_H__

#include <ostream>
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <vector>
#include "include/tinyxml2.h"

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ofstream;

using namespace tinyxml2;

struct RSSItem  // 公开的类
{
    string _title;
    string _link;
    string _description;
    /* string _content; */
};

class FileProcessor
{
public:
    FileProcessor( size_t capa )
    : _capa(capa)
    {
        _rss.reserve(capa);
    }

    // 读文件
    vector<RSSItem> read(const string& filename)
    {
        /* cout << "file" << filename << endl; */
        XMLDocument doc;// 是个类
        //需要关闭文件流吗？
        doc.LoadFile(filename.c_str());
        if(doc.ErrorID())
        {
            std::cerr << "loadFile fail" << endl;
        }

        size_t idx = 0;

        XMLElement* itemNode = 
            doc.FirstChildElement("rss")->FirstChildElement("channel")
            ->FirstChildElement("item");
        // if (itemNode)
        while (itemNode) {
            string title = itemNode->FirstChildElement("title")->GetText();
            string link = itemNode->FirstChildElement("link")->GetText();

            string description;
            if (itemNode->FirstChildElement("description")) {
                description = itemNode->FirstChildElement("description")->GetText();
            }
            /* string content = itemNode->FirstChildElement("content:encoded")->GetText(); */

            // html的标签
            std::regex reg("<[^>]+>"); // 通用正则表达式
            description = regex_replace(description, reg, ""); // regex_replace函数的使用
            /* content = regex_replace(content, reg, ""); */

            RSSItem rssItem;

            rssItem._title = title;
            rssItem._link = link;
            rssItem._description = description;
            /* rssItem._content = content; */

            _rss.push_back(rssItem);
            ++idx;
            

            itemNode = itemNode->NextSiblingElement("item"); //找到下一篇item
        }
        return _rss;
    }


private:
    int _capa;
    vector<RSSItem> _rss;
};

#endif
/* int main(void) */
/* { */
/*     RSS rss(4000); // 4000的预留空间 */
/*     rss.read("人民网语料/auto.xml"); */
/*     rss.store("data/pagelib.dat", "data/offset.dat"); */
/*     return 0; */
/* } */
